package com.huangpuweb.task.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Charsets;
import com.huangpuweb.task.model.QzSysBuildingInfo;
import com.huangpuweb.task.service.SyncQzJGWDataService;
import com.huangpuweb.task.service.SyncSysDataService;
import com.huangpuweb.task.util.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.Date;
import java.util.List;


/**
 * 同步施工工地信息 每周同步一次
 */
@Controller
public class SyncBuildingDataJob {

    private static Logger logger = Logger.getLogger(SyncBuildingDataJob.class);


    private SyncSysDataService syncSysDataService;

    @Autowired
    public void setSyncSysDataService(SyncSysDataService syncSysDataService) {
        this.syncSysDataService = syncSysDataService;
    }


    private SyncQzJGWDataService syncQzJGWDataService;
    @Autowired
    public void setSyncQzJGWDataService(SyncQzJGWDataService syncQzJGWDataService) {
        this.syncQzJGWDataService = syncQzJGWDataService;
    }

    public void SyncBuildingDataJobTask() throws Exception {

        // 创建HttpClient实例
        HttpClient client = HttpClientBuilder.create().build();


        LogFactory.getLog(this.getClass()).info("--------start-------SyncBuildingDataJobTask----------" + new Date());
        System.out.println("--------start-------SyncBuildingDataJobTask----------");

        int pagecount = 100;
        int pagesize = 0;
        int allcount = syncQzJGWDataService.getListBuildingAllCount();
        int pagelastsize = allcount % pagecount;
        if (pagelastsize > 0) {
            pagesize = (int) allcount / pagecount + 1;
        } else {
            pagesize = (int) allcount / pagecount;
        }
        JSONArray ja = new JSONArray();
        JSONObject jo = null;
        JSONObject locationjson = null;
        int id = 0;
        for (int i = 1; i <= pagesize; i++) {
            List<QzSysBuildingInfo> ListsysBuildingInfo = syncQzJGWDataService.getListBuildingByPage(i, pagecount);
            for (QzSysBuildingInfo ou : ListsysBuildingInfo) {


                if (StringUtil.isNotEmpty(ou.getAddress())) {
                    String hpbuildingid = syncSysDataService.getSysHpBuildingIdByUUId(ou.getUuid());
                    if (StringUtils.isNotEmpty(hpbuildingid)) {
                        System.out.println("--------1111111111111111----------");
                        continue;
                    }
                    try {
                        // 根据URL创建HttpGet实例
                        HttpGet get = new HttpGet("http://172.82.4.1:8070/geo-server/public/rest/geocoding/generalSearch?queryStr=" + ou.getAddress());
                        // 执行get请求，得到返回体
                        HttpResponse response = client.execute(get);
                        // 判断是否正常返回
                        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                            // 解析数据

                            String data = EntityUtils.toString(response.getEntity(), Charsets.UTF_8);
                            ja = JSONArray.parseArray(data);
                            jo = ja.getJSONObject(0);
                            locationjson = jo.getJSONObject("location");
                            System.out.println(ou.getName() + "----" + locationjson.getString("x") + "------" + locationjson.getString("y"));
                            ou.setLongitude(locationjson.getString("x"));
                            ou.setLatitude(locationjson.getString("y"));

                        }


                        syncSysDataService.insertNewSysHpBuilding(ou);
                    } catch (Exception e) {
                        continue;
                    }

                }


            }
        }


        LogFactory.getLog(this.getClass()).info("--------end-------SyncBuildingDataJobTask----------" + new Date());
        System.out.println("--------end-------SyncBuildingDataJobTask----------");
    }


}
